const baseUrl = 'http://bdj_khdjk.bemyself.top'  

const request = (url = '', date = {}, type = 'POST') => {
    const Token = uni.getStorageSync('userInfo')
	let header = '';
	if(Token){
		header = { 'X-Token':Token.token }
	}else{
		header = null
	}
    return new Promise((resolve, reject) => {
		uni.showLoading();
        uni.request({
            method: type,
            url: baseUrl + url,
            data: date,
            header: header,
            dataType: 'json',         
        }).then((response) => {
            uni.hideLoading();
            let [error, res] = response;     
            if(res.data.code==-1){//非法请求
                uni.showModal({
                    title: '提示',
                    content: res.data.msg,
                    success: function (res) {
                        if (res.confirm) {
                            console.log('用户点击确定');
                            uni.navigateTo({
                                url:`/pages/login/index`
                            })
                        } else if (res.cancel) {
                            console.log('用户点击取消');
                        }
                    }
                });
                // uni.navigateTo({
                //     url:`/pages/login/index`
                // })
            }
            resolve(res.data);
        }).catch(error => {
            let [err, res] = error;
            reject(err)
        })
    });
}
export default request